home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / whisper / source / bin.c next >
Encoding:
C/C++ Source or Header  |  1991-10-19  |  2.8 KB  |  139 lines

  1. /*
  2.     BinTree
  3. */
  4. #include    <stdio.h>
  5. #include    <string.h>
  6. #include    <ctype.h>
  7.  
  8. #define HASH_MAX    16
  9. #define    HASH(c)        ((c)&15)
  10.  
  11. #define TRUE    1
  12. #define FALSE   0
  13. #define ERR     (-1)
  14.  
  15. typedef struct _DP {
  16.     struct _DP  *left;
  17.     struct _DP  *right;
  18.     int         len;
  19.     char        *str;
  20.     char        *snd;
  21. } DATA;
  22.  
  23. static DATA    *top[HASH_MAX];
  24.  
  25. extern    char   *strdup(char *str);
  26.  
  27. static DATA *serch(str)
  28. char    *str;
  29. {
  30.     int     cd,len;
  31.     DATA    *dp,*bp;
  32.  
  33.     bp = NULL;
  34.     len = 0;
  35.     dp = top[HASH(*str)];
  36.     while ( dp != NULL ) {
  37.         if ( (cd = strncmp(str,dp->str,dp->len)) == 0 && dp->len > len ) {
  38.         len = dp->len;
  39.         bp = dp;
  40.         } else if ( cd > 0 )
  41.             dp = dp->left;
  42.         else
  43.             dp = dp->right;
  44.     }
  45.     return bp;
  46. }
  47. void    TALK_str(str)
  48. char    *str;
  49. {
  50.     int     n;
  51.     DATA    *dp;
  52.  
  53.     while ( *str != '\0' ) {
  54.     if ( (dp = serch(str)) != NULL ) {
  55.         PLAY_snd(dp->snd);
  56.         n = dp->len;
  57.     } else if ( iskan(str) )
  58.         n = 2;
  59.     else
  60.         n = 1;
  61.     str += n;
  62.     }
  63. }
  64. DATA    *TALK_new(str,snd)
  65. char    *str,*snd;
  66. {
  67.     int     cd;
  68.     DATA    *tp;
  69.     DATA    tmp;
  70.     register DATA *dp;
  71.  
  72.     cd = 1;
  73.     tmp.left = top[HASH(*str)];
  74.     dp = &tmp;
  75.     for ( ; ; ) {
  76.         if ( cd > 0 ) {
  77.             if ( dp->left == NULL )
  78.                 break;
  79.             dp = dp->left;
  80.         } else {
  81.             if ( dp->right == NULL )
  82.                 break;
  83.             dp = dp->right;
  84.         }
  85.         if ( (cd = strcmp(str,dp->str)) == 0 )
  86.             return dp;
  87.     }
  88.     if ( (tp = (DATA *)malloc(sizeof(DATA))) == NULL )
  89.         return NULL;
  90.  
  91.     tp->left = tp->right = NULL;
  92.     tp->len = strlen(str);
  93.     tp->str = strdup(str);
  94.     tp->snd = strdup(snd);
  95.  
  96.     if ( cd > 0 )
  97.         dp->left = tp;
  98.     else
  99.         dp->right = tp;
  100.  
  101.     top[HASH(*str)] = tmp.left;
  102.  
  103.     return tp;
  104. }
  105. int     TALK_init(file)
  106. char    *file;
  107. {
  108.     FILE    *fp;
  109.     char    *str,*snd;
  110.     char    buf[BUFSIZ];
  111.     register char *p;
  112.  
  113.     sprintf(buf,"%s%s",macget("HOME"),file);
  114.     if ( (fp = fopen(buf,"r")) == NULL )
  115.         return ERR;
  116.  
  117.     while ( fgets(buf,BUFSIZ,fp) != NULL ) {
  118.         if ( buf[0] == '#' || buf[0] == '\n' )
  119.             continue;
  120.         if ( (p = strchr(buf,'\n')) != NULL )
  121.             *p = '\0';
  122.         p = buf;
  123.         while ( isspace(*p) ) p++;
  124.         str = p;
  125.         while ( !isspace(*p) && *p != '\0' ) p++;
  126.         if ( *p != '\0' )
  127.             *(p++) = '\0';
  128.         while ( isspace(*p) ) p++;
  129.         snd = p;
  130.         if ( *str != '\0' && *snd != '\0' &&
  131.              TALK_new(str,snd) == NULL ) {
  132.             fclose(fp);
  133.             return ERR;
  134.         }
  135.     }
  136.     fclose(fp);
  137.     return FALSE;
  138. }
  139.